package zee.com.customviewexample.activity

import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import com.zee.adapter.OnItemClickListener
import com.zee.adapter.RecyclerViewViewAdapter
import com.zee.adapter.SimpleZxBannerAdapter
import com.zee.base.BaseZAdapter
import com.zee.base.Z1RecyclerAdapter
import com.zee.utils.UIUtils
import com.zee.view.ZxBannerView
import kotlinx.android.synthetic.main.activity_zx_recyclerview.*
import zee.com.customviewexample.R

class ZxRecyclerViewActivity : BaseActivity() {

    override fun getLayoutID(): Int {
        return R.layout.activity_zx_recyclerview
    }

    override fun initViews() {
//        val data = MoreData(custom_zxRecyclerView, this, list);
//        adapter = data.adapter;
        val list = ArrayList<PackageInfo>()
        UIUtils.postDelayed({
            val installedPackages = packageManager.getInstalledPackages(PackageManager.GET_ACTIVITIES)
            installedPackages.forEach {
                if (it.packageName.contains("com.android") || it.packageName.contains("com.huawei")) {
                } else {
                    list.add(it);
                }
            }
            val list2 = ArrayList<PackageInfo>();
            list2.addAll(list)
            custom_zxRecyclerView.addHeaderView(object : RecyclerViewViewAdapter() {
                override fun getLayoutResID(): Int {
                    return R.layout.view_zx_recycler_head
                }

                override fun initViews() {
                    var array = intArrayOf(R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher)
                    val id_banner_view = findViewById<ZxBannerView>(R.id.zv_banner_a)
                    id_banner_view.setBannerAdapter(object : SimpleZxBannerAdapter<Int>(array.toList()) {
                        override fun initViews(imageView: ImageView, position: Int) {
                            imageView.setImageResource(array[position])
                        }
                    })
//                    val mWebView = findViewById<WebView>(R.id.zv_webView)
//                    mWebView.getSettings().setJavaScriptEnabled(true)
//                    mWebView.setWebViewClient(WebViewClient())
//                    mWebView.setWebChromeClient(WebChromeClient())
//                    //        mWebView.loadUrl("https://xw.qq.com/partner/hwbrowser/20190731A0HV99/20190731A0HV9900?ADTAG=hwb&pgv_ref=hwb&appid=hwbrowser&ctype=news");
//                    mWebView.loadUrl("https://www.csdn.net/nav/mobile")
                }

            })


//            adapter.setList(list2.subList(0, 2))
            val adapter = object : Z1RecyclerAdapter<PackageInfo>(R.layout.activity_zx_foldtextview) {

                override fun initViews(parentView: View, location: Int, bean: PackageInfo) {
                    bean.apply {

                    }
                }
            }
            custom_zxRecyclerView.adapter = adapter

            adapter.setItemClickListener(object : OnItemClickListener<PackageInfo> {
                override fun onItemClick(bean: PackageInfo, position: Int) {

                }

            })
            custom_zxRecyclerView.setRefreshListener {
                UIUtils.postDelayed({
                    custom_zxRecyclerView.setList(list2)
                    custom_zxRecyclerView.loadFinish()
                }, 3000)
            }

        }, 1)

        //有数据
//        id_have_data.setOnClickListener {
//            adapter.setList(ArrayList<PackageInfo>(list))
//        }
//        addNoNetViewById();
        testNoDataWithLayoutID();

    }

    fun refresh() {
//        custom_zxRecyclerView.addHeadEmptyView(100)
//        custom_zxRecyclerView.addFootEmptyView(100)

//        custom_zxRecyclerView.setRefreshAndLoadMordListener(object : RefreshAndLoadMordListener {
//            override fun onLoadMore() {
//                UIUtils.postDelayed({
////                    custom_zxRecyclerView.loadFinish()
////                    custom_zxRecyclerView.setLoadMoreEnabled(false)
//
//                }, 4000)
//            }
//
//            override fun onRefresh() {
//                UIUtils.postDelayed({ custom_zxRecyclerView.loadFinish() }, 4000)
//            }
//        })
//        custom_zxRecyclerView.addHeadEmptyView(20).setBackgroundColor(Color.parseColor("#ffff0000"))
//        custom_zxRecyclerView.addHeadEmptyView(5).setBackgroundColor(Color.parseColor("#ffffffff"))
//        custom_zxRecyclerView.addFootEmptyView(5).setBackgroundColor(Color.parseColor("#ffff0000"))
//        custom_zxRecyclerView.addFootEmptyView(10).setBackgroundColor(Color.parseColor("#ffff00ff"))
    }

    /**
     * 显示LayouId的View
     */
    fun addNoNetViewById() {
        //没有网络
        id_no_net.setOnClickListener {
            //            adapter.clear();
            UIUtils.postDelayed({
                custom_zxRecyclerView.setShowCenterView(1)
            }, 100)
        }

        custom_zxRecyclerView.addCenterView(object : RecyclerViewViewAdapter(1) {
            override fun getLayoutResID(): Int {
                return R.layout.item_no_net
            }

            override fun initViews() {
                findViewById<TextView>(R.id.test_button).setOnClickListener { UIUtils.showToastShort("别点了，当前没有网络") }
            }
        })
    }

//    fun addHeadViews(number: Int) {
////        for (i in 0..number) {
////            val noBodyTV = TextView(this)
////            noBodyTV.setText("添加到头部" + i)
////            custom_zxRecyclerView.addHeaderView(noBodyTV)
////        }
////        custom_zxRecyclerView.addHeadEmptyView(200)
////        custom_zxRecyclerView.addFootEmptyView(200)
//
//    }

    fun addFootView() {
        val textView: TextView = TextView(this);
        textView.setBackgroundColor(UIUtils.getColor(R.color.color_333))
        val layout = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        layout.apply {
            width = ViewGroup.LayoutParams.MATCH_PARENT;
            height = UIUtils.dpToPx(300)
        }
        textView.layoutParams = layout;

        val textView1: TextView = TextView(this);
        textView1.setBackgroundColor(UIUtils.getColor(R.color.colorPrimaryDark))
        val layout1 = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        layout1.apply {
            width = ViewGroup.LayoutParams.MATCH_PARENT;
            height = UIUtils.dpToPx(100)
        }
        textView1.layoutParams = layout1;
        custom_zxRecyclerView.addFooterView(textView);
        custom_zxRecyclerView.addFooterView(textView1);
    }

    /**
     * 没有数据的时候设置LayouID
     */
    fun testNoDataWithLayoutID() {
        //没有数据
        id_no_data.setOnClickListener {
            custom_zxRecyclerView.baseRVAdapter.clear()
            custom_zxRecyclerView.noDataShowNoDataView()
        }
//        custom_zxRecyclerView.setEmptyView(object : RecyclerViewViewAdapter(0) {
//            override fun getLayoutResID(): Int {
//                return R.layout.item_empty;
//            }
//
//
//            override fun initViews() {
//                findViewById<TextView>(R.id.test_button).setOnClickListener { UIUtils.showToastShort("点击成功") }
//            }
//        })
    }

}
